﻿using System;
using System.Collections.Generic;
 
using System.Text;
using System.Windows.Forms;

namespace Skipette
{
    public class Conversation
    {
        public int convId;
        List<Account> participants = new List<Account>();
        public bool call;
        public bool video;
        bool popped = false;
        Queue<String> rawMessagesWaiting = new Queue<string>();

        public Conversation()
        {
            Random r = new Random();
            convId = r.Next(Int32.MaxValue);
            participants.Add(Program.currentProfile);
        }

        public Conversation(int convId)
        {
            participants.Add(Program.currentProfile);
            this.convId = convId;
        }

        public void SendMessage(Account author, RichTextBox tb, String message)
        {
            Program.st.main.ShowMessage(convId, author.username, DateTime.Now.ToString(), message); 
            //tb.Text += "\n" + Program.currentProfile.firstName + " (" + Program.currentProfile.username + ") " + DateTime.Now;
            //tb.Text += "\n  " + message + "\n";
            foreach (Account fr in participants)
            {
                Server.SendMessageTo(fr, message, convId);
            }
        }

        public void ReceiveMessage(String rawMessage)
        {
            if (popped)
                rawMessagesWaiting.Enqueue(rawMessage);
            else
                ShowRawMessage(rawMessage);
        }

        private void ShowRawMessage(String rawMessage)
        {
                String author = "";
                String dateTime = "";
                String message = "";
                String[] splitRes = rawMessage.Split(new String[] { "*SKBREAKER*" }, StringSplitOptions.None);
                author = splitRes[1];
                dateTime = splitRes[2];
                message = splitRes[3];

                Program.st.main.ShowMessage(Int32.Parse(splitRes[0]), author, dateTime, message);
        }

        public void See()
        {
            popped = false;
            foreach (String rawMessage in rawMessagesWaiting)
                ShowRawMessage(rawMessage);
        }
        
    }
}
